package myjme;


import Globals.AllGlobal;
import brain.MyBrain;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.export.binary.BinaryExporter;
import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.Trigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Spatial;
import com.jme3.scene.VertexBuffer;
import com.jme3.scene.shape.Box;
import com.jme3.util.BufferUtils;
import controller.MyController;
import database.operate.OperateMyDatabaseImpl;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.GongZuoMian_Model;
import model.ZuanChang_Model;
import model.ZuanKong_Model;
import myjme.appstate.DiCengAppState;
import myjme.appstate.GuiAppState;
import myjme.appstate.GongZuoMianAppState;
import myjme.appstate.LittleMapAppState;
import myjme.appstate.ZuanKongAppState_Cuboid;
import myjme.appstate.ZuanKongAppState_Cylinder;
import myjme.appstate.plainappstate.PlainAppstate;
import myjme.tin.core.GridController;

public class JmeCanvas extends SimpleApplication {
    /**
     * 下面是控制器，非jme里面的
     */
    private MyBrain brain;
    private MyController controller;
    /**
     * 创建私有zuankong模型
     */
    private int myspeed=8;
    private ZuanKong_Model zuanKong_Model;
    private ZuanChang_Model zuanChang_Model;
    private GongZuoMian_Model gongZuoMian_Model;
    private GongZuoMianAppState gzmAppState=new GongZuoMianAppState();
    private DiCengAppState diCengAppState=new DiCengAppState();
    private GuiAppState guiAppState=new GuiAppState();
    private ZuanKongAppState_Cylinder zuanKongAppState=new ZuanKongAppState_Cylinder();
    private ZuanKongAppState_Cuboid zuanKongAppState_Cuboid=new ZuanKongAppState_Cuboid();
    private LittleMapAppState littleMapAppState=new LittleMapAppState();
    private PlainAppstate plainAppstate=new PlainAppstate();
    private String VIEW_SWITCH_MAPPING=java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("VIEW_SWITCH");
    private String LOAD_GZM_MAPPING=java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("LOAD_GZM");
    private String LOAD_ZC_MAPPING=java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("LOAD_ZC");
    private String LOAD_ZK_MAPPING=java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("LOAD_ZK");
    private String LOAD_ZKALL_MAPPING=java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("LOAD_ZKALL");
    private String LOAD_LITTLE_MAP_MAPPING=java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("LOAD_LITTLE_MAP");
    private String LOAD_TWO_D_MAPPING=java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("LOAD_TWO_D");
    private String SPEED_UP=java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("SPEEDUP");
    private String SPEED_DOWN=java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("SPEEDDOWN");
    private Trigger LOAD_GZM_TRIGGER=new KeyTrigger(KeyInput.KEY_G);
    private Trigger LOAD_ZC_TRIGGER=new KeyTrigger(KeyInput.KEY_C);
    private Trigger LOAD_ZK_TRIGGER=new KeyTrigger(KeyInput.KEY_K);
    private Trigger LOAD_ZKALL_TRIGGER=new KeyTrigger(KeyInput.KEY_L);
    private Trigger LOAD_LITTLE_MAP_TRIGGER=new KeyTrigger(KeyInput.KEY_P);
    private Trigger LOAD_TWO_D_TRIGGER=new KeyTrigger(KeyInput.KEY_H);
    private Trigger SPEED_UP_TRIGGER=new KeyTrigger(KeyInput.KEY_PGUP);
    private Trigger SPEED_DOWN_TRIGGER=new KeyTrigger(KeyInput.KEY_PGDN);
    private Trigger VIEW_SWITCH_Trigger=new KeyTrigger(KeyInput.KEY_V);
    private int ZK_DRAW_TYPE= AllGlobal.DRAW_ONE;
    private int ZC_DRAW_TYPE= AllGlobal.DRAW_ONE;
    private int GZM_DRAW_TYPE= AllGlobal.DRAW_ONE;
    private HashMap<Integer,HashMap<AbstractAppState,Boolean>> showStateHM
            =new HashMap<Integer,HashMap<AbstractAppState,Boolean>>();
    private HashMap<AbstractAppState,Boolean> threeD=new HashMap<AbstractAppState,Boolean>();
    private HashMap<AbstractAppState,Boolean> twoD=new HashMap<AbstractAppState,Boolean>();
    private boolean VIEW_3D=true;
    
    public void simpleInitApp() {
        
        
        
        
        DirectionalLight sunDirectionalLight=new DirectionalLight();
        sunDirectionalLight.setDirection(new Vector3f(-0.39f, -0.32f, -0.74f));
        rootNode.addLight(sunDirectionalLight);
        
        
        setDisplayStatView(false);
        guiFont = assetManager.loadFont("Interface/mychinese/myfont.fnt");
        final BitmapText helloText = new BitmapText(guiFont, false);
        helloText.setSize(16);
        helloText.setText("你正处于3D界面，绘制快捷键G，绘制钻孔快捷键L");
        helloText.setColor(new ColorRGBA(0.34f, 0.25f, 0.88f, 0.9f));
        helloText.setLocalTranslation(10, 600, 0);
        guiNode.attachChild(helloText);
        
        flyCam.setDragToRotate(true);
        viewPort.setBackgroundColor(new ColorRGBA(170.0f/255.0f, 212.0f/255.0f, 255.0f/255.0f, 1));
        //new ColorRGBA(237.0f/255.0f, 222.0f/255.0f, 139.0f/255.0f, 1)
        //添加state
        inputManager.addMapping(LOAD_ZC_MAPPING, LOAD_ZC_TRIGGER);
        inputManager.addMapping(LOAD_ZK_MAPPING, LOAD_ZK_TRIGGER);
        inputManager.addMapping(LOAD_GZM_MAPPING, LOAD_GZM_TRIGGER);
        inputManager.addMapping(LOAD_ZKALL_MAPPING, LOAD_ZKALL_TRIGGER);
        inputManager.addMapping(LOAD_LITTLE_MAP_MAPPING, LOAD_LITTLE_MAP_TRIGGER);
        inputManager.addMapping(SPEED_UP, SPEED_UP_TRIGGER);
        inputManager.addMapping(SPEED_DOWN, SPEED_DOWN_TRIGGER);
        inputManager.addMapping(LOAD_TWO_D_MAPPING, LOAD_TWO_D_TRIGGER);
        inputManager.addMapping(VIEW_SWITCH_MAPPING, VIEW_SWITCH_Trigger);
        ActionListener actionListener=new ActionListener() {
            
            public void onAction(String name, boolean isPressed, float tpf) {
                if(name.equals(VIEW_SWITCH_MAPPING)&&isPressed){
                    if(VIEW_3D){
                        controller.getDco().setVisible(true);
                        VIEW_3D=false;
                        viewPort.setBackgroundColor(ColorRGBA.White);
                        controller.setViewShow(AllGlobal.VIEW_TWO_D);
                                /** Write text on the screen (HUD) */
                        helloText.setText("你正处于2D界面,绘制快捷键H");
                        cam.setLocation(new Vector3f(0, 0, 10));
                    }else{
                        controller.getDco().setVisible(false);
                        VIEW_3D=true;
                        viewPort.setBackgroundColor(new ColorRGBA(170.0f/255.0f, 212.0f/255.0f, 255.0f/255.0f, 1));
                        controller.setViewShow(AllGlobal.VIEW_THREE_D);
                        helloText.setText("你正处于3D界面，绘制快捷键G，绘制钻孔快捷键L");
                        cam.setLocation(new Vector3f(0, 0, 10));
                    }
                }
                if(controller.getViewShow()==AllGlobal.VIEW_THREE_D){
                    plainAppstate.cleanup();
                    if(name.equals(LOAD_GZM_MAPPING)&&isPressed){
                        try {
                            diCengAppState.setDiCengSize();
                            diCengAppState.drawDiCeng_LingYiGe();  
                            guiAppState.drawInfoPic();
                            //plainAppstate.stateDetached(stateManager);
                            
                            
                        } catch (Exception ex) {
                            Logger.getLogger(JmeCanvas.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        drawGZM(gongZuoMian_Model,GZM_DRAW_TYPE);
                        drawZKAll();
                        littleMapAppState.setZC(controller.getGZM_TYPE());
                        if(controller.getZuankongType()==AllGlobal.CYLINDER_TYPE){
                            if(stateManager.getState(ZuanKongAppState_Cylinder.class)!=null){
                                if(stateManager.getState(ZuanKongAppState_Cuboid.class)!=null){
                                    stateManager.detach(zuanKongAppState_Cuboid);
                                }
                            }else{
                                stateManager.attach(zuanKongAppState);
                                if(stateManager.getState(ZuanKongAppState_Cuboid.class)!=null){
                                    stateManager.detach(zuanKongAppState_Cuboid);
                                }
                            }
                        }else if(controller.getZuankongType()==AllGlobal.CUBOID_TYPE){
                            if(stateManager.getState(ZuanKongAppState_Cuboid.class)!=null){
                                if(stateManager.getState(ZuanKongAppState_Cylinder.class)!=null){
                                    stateManager.detach(zuanKongAppState);
                                }
                            }else{
                                stateManager.detach(zuanKongAppState);
                                if(stateManager.getState(ZuanKongAppState_Cylinder.class)!=null){
                                    stateManager.detach(zuanKongAppState);
                                }
                            }                        
                        }
                    }else if(name.equals(LOAD_ZC_MAPPING)&&isPressed){
                        drawZC(zuanChang_Model,ZC_DRAW_TYPE);
                    }else if(name.equals(LOAD_ZK_MAPPING)&&isPressed){
                        drawZK(zuanKong_Model,ZK_DRAW_TYPE);
                    }else if(name.equals(LOAD_ZKALL_MAPPING)&&isPressed){
                        drawZKAll();
                        
                        
                    }else if(name.equals(LOAD_LITTLE_MAP_MAPPING)&&isPressed){
                        littleMapAppState.setZC(controller.getGZM_TYPE());
                    }else if(name.equals(SPEED_UP)&&isPressed){
                        flyCam.setMoveSpeed(myspeed++);
                    }else if(name.equals(SPEED_DOWN)&&isPressed){
                        flyCam.setMoveSpeed(myspeed--);                    
                    }
                }else if(controller.getViewShow()==AllGlobal.VIEW_TWO_D){
                    gzmAppState.cleanup();
                    diCengAppState.cleanup();
                    zuanKongAppState.cleanup();
                    zuanKongAppState_Cuboid.cleanup();
                    littleMapAppState.cleanup();
                    guiAppState.cleanup();
                    guiAppState.updateTheGuiNode(false);
                    if(name.equals(LOAD_TWO_D_MAPPING)&&isPressed){
                        plainAppstate.drawAll();
                    }
                }                
            }
        };
        inputManager.addListener(actionListener, 
                new String[]{VIEW_SWITCH_MAPPING,LOAD_TWO_D_MAPPING,LOAD_GZM_MAPPING,LOAD_ZC_MAPPING,LOAD_ZK_MAPPING,LOAD_ZKALL_MAPPING,LOAD_LITTLE_MAP_MAPPING,SPEED_UP,SPEED_DOWN});
        stateManager.attachAll(gzmAppState,guiAppState,zuanKongAppState,littleMapAppState,zuanKongAppState_Cuboid,diCengAppState,plainAppstate);
        
        AmbientLight al=new AmbientLight();
        al.setColor(ColorRGBA.White);
        rootNode.addLight(al);
        
        flyCam.setMoveSpeed(18);
        cam.setLocation(new Vector3f(0, 0, 10));	
    }
    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
        
    }
    @Override
    /**
     * 这里主要是提供保存上一次进度功能
     */
    public void stop(){
        String userHome=System.getProperty(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("USER.HOME"));
        File file=new File(userHome+java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("/SAVEDGAMES/")+java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("SAVEDGAME.J3O"));
        BinaryExporter exporter=BinaryExporter.getInstance();
        try {
            exporter.save(rootNode, file);
        } catch (Exception e) {
            Logger.getLogger(JmeCanvas.class.getName()).log(Level.SEVERE, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("THAT IS WRONG FOR ME TO SAVE MODEL"),e);
        }
        super.stop();
    }
    /**设置模型，只有那几个Dialog可以调用
     */
    public void setGongZuoMian(GongZuoMian_Model gongZuoMian_Model,int drawType){
        this.gongZuoMian_Model=gongZuoMian_Model;
        this.GZM_DRAW_TYPE=drawType;
    }
    public void setZuanChang(ZuanChang_Model zuanChang_Model,int drawType){
        this.zuanChang_Model=zuanChang_Model;
        this.ZC_DRAW_TYPE=drawType;
    }
    public void setZuanKong(ZuanKong_Model zuanKong_Model,int drawType){
        this.zuanKong_Model=zuanKong_Model;
        this.ZK_DRAW_TYPE=drawType;
    }
    /**
     * 添加注浆孔，主要传入的参数钻孔数据模型
     */
    public void drawZK(ZuanKong_Model zuanKong_Model,int drawType){
        if(zuanKong_Model!=null){
            if(controller.getZuankongType()==AllGlobal.CYLINDER_TYPE){
                zuanKongAppState.drawZK(zuanKong_Model,drawType);
                ZK_DRAW_TYPE=AllGlobal.UPDATE_ONE;
            }else if(controller.getZuankongType()==AllGlobal.CUBOID_TYPE){
                zuanKongAppState_Cuboid.drawZK(zuanKong_Model,drawType);
                ZK_DRAW_TYPE=AllGlobal.UPDATE_ONE;
            }            
        }     
    }
    public void drawZKAll(){
        try {
            if(controller.getZuankongType()==AllGlobal.CYLINDER_TYPE){
                zuanKongAppState.drawZKAll();
            }else if(controller.getZuankongType()==AllGlobal.CUBOID_TYPE){
                zuanKongAppState_Cuboid.drawZKAll();
            }
        } catch (Exception ex) {
            Logger.getLogger(JmeCanvas.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void drawZC(ZuanChang_Model zuanChang_Model,int drawType){
        if(zuanChang_Model!=null){
            zuanKongAppState.drawZC(zuanChang_Model,drawType);
            ZK_DRAW_TYPE=AllGlobal.UPDATE_ONE;
        }
        
    }//这个直接附加在巷道处
    public void drawGZM(GongZuoMian_Model gongZuoMian_Model,int drawType){
        if(gongZuoMian_Model!=null){
            gzmAppState.drawHangDao(Float.parseFloat(
                gongZuoMian_Model.getGzmcd()), 
                Float.parseFloat(gongZuoMian_Model.getGzmzxcdString()),
                gongZuoMian_Model.getMingcheng(),drawType);
            GZM_DRAW_TYPE=AllGlobal.UPDATE_ONE;
            System.out.println(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("WOSHIDRAWGZM1"));
        }else{
            try {
                ArrayList<GongZuoMian_Model> alist= OperateMyDatabaseImpl.getAllResult(AllGlobal.GONGZUOMIAN_SQL);
                if(alist.size()>0){
                    System.out.println(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("WOSHIDRAWGZM2"));
                    GongZuoMian_Model temp=alist.get(0);
                    gzmAppState.drawHangDao(Float.parseFloat(
                        temp.getGzmcd()), 
                        Float.parseFloat(temp.getGzmzxcdString()),
                        temp.getMingcheng(),drawType);
                    GZM_DRAW_TYPE=AllGlobal.UPDATE_ONE;
                }else{
                    gzmAppState.drawHangDao(0,0,java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("无"),drawType);
                }                                                  
            } catch (Exception ex) {
                Logger.getLogger(JmeCanvas.class.getName()).log(Level.SEVERE, null, ex);
            }
        }        
    }
    /**
     * 添加全局控制器
     */
    public void addController(MyController controller){
        this.controller=controller;
        guiAppState.addController(this.controller);
        diCengAppState.addController(this.controller);
        gzmAppState.addController(this.controller);
        zuanKongAppState.addController(this.controller);
        zuanKongAppState_Cuboid.addController(this.controller);
        littleMapAppState.addController(this.controller);
        plainAppstate.addController(this.controller);
        int viewShow=this.controller.getViewShow();
        
        threeD.put(gzmAppState, true);
        int zuankongType=controller.getZuankongType();
        threeD.put(zuanKongAppState, zuankongType==AllGlobal.CYLINDER_TYPE?true:false);
        threeD.put(zuanKongAppState_Cuboid, zuankongType==AllGlobal.CYLINDER_TYPE?false:true);
        threeD.put(guiAppState, true);
        threeD.put(littleMapAppState, true);
        twoD.put(plainAppstate, false);

        showStateHM.put(AllGlobal.VIEW_THREE_D, threeD);
        showStateHM.put(AllGlobal.VIEW_TWO_D, twoD);
    }

    /**添加大脑*/
    public void addBrain(MyBrain brain){
        this.brain=brain;
        diCengAppState.addBrain(this.brain);
        guiAppState.addBrain(this.brain);
        gzmAppState.addBrain(this.brain);
        zuanKongAppState.addBrain(this.brain);
        zuanKongAppState_Cuboid.addBrain(this.brain);
        plainAppstate.addBrain(this.brain);
    }
	
    public void useForFXOper(final int sjType,final float ycjl){
        if(!VIEW_3D){
            
            this.enqueue(new Callable<AbstractAppState>() {
                public AbstractAppState call() throws Exception {
                    return plainAppstate.setSD(sjType, ycjl);
                }
            });
        }else{
            
        }
        
    }

    public boolean isVIEW_3D() {
        return VIEW_3D;
    }
    
    public boolean deleteOne(int sqlType,String name){
       return true;
    }
    
    public boolean addOne(int sqlType,String name){
        return true;
    }
    
    public boolean updateOne(int sqlType,String name){
        return true;
    }
    
    public boolean searchOne(int sqlType,String name){
        return true;
    }
    
    public boolean deleteAll(int sqlType,ArrayList<String> list){
       return true;
    }
    
    public boolean addAll(int sqlType,ArrayList<String> list){
        return true;
    }
    
    public boolean updateAll(int sqlType,ArrayList<String> list){
        return true;
    }
    
    public boolean searchAll(int sqlType,ArrayList<String> list){
        return true;
    }
    
    public boolean deleteMany(int sqlType,ArrayList<String> list){
       return true;
    }
    
    public boolean addMany(int sqlType,ArrayList<String> list){
        return true;
    }
    
    public boolean updateMany(int sqlType,ArrayList<String> list){
        return true;
    }
    
    public boolean searchMany(int sqlType,ArrayList<String> list){
        return true;
    }
}
